//
//  ViewController.m
//  20503
//
//  Created by student on 16/3/22.
//  Copyright © 2016年 niit. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()
@property (nonatomic,strong) NSArray *arr;
@property (weak, nonatomic) IBOutlet UIActivityIndicatorView *ActivityView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    
self.arr = @[@"http://www.apple.com.cn/mac-pro/images/static/processor.jpg",@"http://www.apple.com.cn/mac-pro/images/static/memory.jpg",@"http://www.apple.com.cn/mac-pro/images/static/graphics.jpg",@"http://www.apple.com.cn/mac-pro/images/static/storage.jpg",@"http://www.apple.com.cn/mac-pro/images/static/thermal.jpg",@"http://www.apple.com.cn/mac-pro/images/static/fan.jpg",@"http://www.apple.com.cn/mac-pro/images/static/expansion.jpg",@"http://www.apple.com.cn/mac-pro/images/static/wifi.jpg",@"http://www.apple.com.cn/mac-pro/images/static/design.jpg",@"http://www.apple.com.cn/mac-pro/images/static/comingsoon.jpg"];
    
    // 练习要求:
    // 使用gcd及performSelectorInBackground方法实现多线程下载图片并显示到界面上,不能卡主界面,并且gcd采用不同的方式(串行、并发)都实现一下
}

- (IBAction)btn1Pressed:(id)sender
{
    // 网址字符串
    NSString *urlStr = @"http://www.apple.com.cn/mac-pro/images/static/processor.jpg";
    // 网址对象
    NSURL *url = [NSURL URLWithString:urlStr];
    // 获取数据
    NSData *data = [NSData dataWithContentsOfURL:url];
    // 图像对象
    UIImage *image = [UIImage imageWithData:data];
    // 图像视图
    UIImageView *imgView = [[UIImageView alloc] initWithImage:image];
    imgView.frame = CGRectMake(100, 100, 100, 100);
    // 添加到视图
    [self.view addSubview:imgView];
}

- (IBAction)btn2Pressed:(id)sender
{
    // 开始转动
    [self.ActivityView startAnimating];
    
    // GCD方式
    // 1. 定义一个队列(自定义队列或全局队列)
    dispatch_queue_t queue = dispatch_queue_create(NULL, NULL);
    // 2. 将要做的任务使用dispatch_async放到队列中
    dispatch_async(queue, ^{
                dispatch_async(dispatch_get_main_queue(), ^{
                    for (int i = 0;i<self.arr.count;i++)
                    {
                    NSMutableArray *mArr = [NSMutableArray array];
                    UIImageView *image = [self addImage:i andX:i];
                    [mArr addObject:image];
                    for (int i = 0; i < mArr.count;i++)
                    {
                        
                        UIImageView *images = mArr[i];
                        [self.view addSubview:images];
                    }
                    [self.ActivityView stopAnimating];
                }

            [self.ActivityView stopAnimating];
        });
        
    });
}

- (IBAction)Btn3Pressed:(id)sender

{
    // 开始的时间
    NSDate *d1 = [NSDate date];
    // 开始转动
    [self.ActivityView startAnimating];
    
    
    // GCD方式
    // 1. 定义一个队列(自定义队列或全局队列)
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);// 并发
    //dispatch_queue_create(NULL, NULL);
    // 2. 将要做的任务使用dispatch_async放到队列中
    dispatch_async(queue, ^{
        
        
        
        // 定义一个组
   

//        
//        dispatch_group_async(group, queue, ^{
//            
//        // 等待组内的任务都完成后，继续运行
//        dispatch_group_wait(group, DISPATCH_TIME_FOREVER);
        
        // 3. 得到计算结果后，将更新界面的代码放入到主队列中执行
        dispatch_async(dispatch_get_main_queue(), ^{
            NSLog(@"当前在%@",[NSThread currentThread].isMainThread?@"主线程中":@"子线程中");
            
            NSMutableArray *mArr = [NSMutableArray array];
            
            dispatch_group_t group = dispatch_group_create();
            
            for (int i = 0;i<self.arr.count;i++) {
           
                dispatch_group_async(group, queue, ^{
                    UIImageView *image = [self addImage:i andX:i];
                    [mArr addObject:image];
            //[mArr addObject:imgView];
                    });
                
                            }
            dispatch_group_wait(group, DISPATCH_TIME_FOREVER);
            dispatch_async(dispatch_get_main_queue(), ^{
                for (int i = 0; i < mArr.count;i++)
                {
                    
                    UIImageView *images = mArr[i];
                    [self.view addSubview:images];
                    // 停止转动
                }

            [self.ActivityView stopAnimating];
            
            // 运行完时间
            NSDate *d2 = [NSDate date];
            // 计算总共运行了多少时间
            NSTimeInterval interval = [d2 timeIntervalSinceDate:d1];
            NSLog(@"运行了%.3f秒",interval);
        });
          });
    });
    
}


- (IBAction)btn5Pressed:(id)sender
{
    // 把某个任务放到后台线程执行
    [self.ActivityView startAnimating];
    
    [self performSelectorInBackground:@selector(calculate) withObject:nil];
}

- (void)calculate
{
        // 更新界面又得回到主线程
    
    
        [self performSelectorOnMainThread:@selector(showImages) withObject:nil waitUntilDone:NO];
        [self.ActivityView performSelectorOnMainThread:@selector(stopAnimating) withObject:nil waitUntilDone:NO];
    
    
}
- (IBAction)btn4Pressed:(id)sender
{
    NSDate *d1 = [NSDate date];
    
    // 开始转动
    [self.ActivityView startAnimating];
    
    // GCD方式
    // 1. 定义一个队列(自定义队列或全局队列)
    dispatch_queue_t queue = dispatch_queue_create(NULL, NULL);
    // 2. 将要做的任务使用dispatch_async放到队列中
    dispatch_async(queue, ^{
        NSMutableArray *mArr = [NSMutableArray array];
        
        for (int i = 0;i<self.arr.count;i++) {
            
            UIImageView *image = [self addImage:i andX:i];
            [mArr addObject:image];
            
        }
        // 3. 得到计算结果后，将更新界面的代码放入到主队列中执行
        dispatch_async(dispatch_get_main_queue(), ^{
            NSLog(@"当前在%@",[NSThread currentThread].isMainThread?@"主线程中":@"子线程中");
            // 显示结果
            for (int i = 0; i < mArr.count;i++)
            {
                
                UIImageView *images = mArr[i];
                [self.view addSubview:images];
            }

            // 停止转动
            [self.ActivityView stopAnimating];
        });
        
    });
    
    
    NSDate *d2 = [NSDate date];
    
    NSTimeInterval t = [d2 timeIntervalSinceDate:d1];
    NSLog(@"btn4Pressed 耗费时间:%.3f",t);
}


- (UIImageView *)addImage:(int)imageName andX:(int)x
{
    
    NSString *urlStr = self.arr[imageName];
    NSURL *url = [NSURL URLWithString:urlStr];

    NSData *data = [NSData dataWithContentsOfURL:url];

    UIImage *image = [UIImage imageWithData:data];
    
    UIImageView *imgView = [[UIImageView alloc] initWithImage:image];

    int y = 300;
    if (x < 3)
    {
        x  -=  0;
        y = 300;
        
    }
    else if(x >= 3 && x < 6)
    {
        x -= 3;
        y = 400;
    }
    else if(x >= 6 && x < 9)
    {
        x -= 6;
        y = 500;
    }
    else
    {
        x -= 9;
        y = 600;
    }

    imgView.frame = CGRectMake(x*100, y, 100, 100);
    
    
    return imgView;
        
}

- (void)showImages
{
    for (int i = 0;i<self.arr.count;i++)
    {
        NSMutableArray *mArr = [NSMutableArray array];
        UIImageView *image = [self addImage:i andX:i];
        [mArr addObject:image];
        for (int i = 0; i < mArr.count;i++)
        {
            
            UIImageView *images = mArr[i];
            [self.view addSubview:images];
        }
        [self.ActivityView stopAnimating];
}
}
@end
